SPA_RESOLVE_INIT = function(transition) {
    getHomework()
}

function getHomework() {
    $.ajax({
        url:get_homework_interface,
        type:'post',
        dataType:'json',
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:{
            homeworkId: $.getQueryString("homeworkId")
        },
        success:function (data) {
            if(data.code==200){
                startTimes=data.data.start_time.split(" ")
                endTimes=data.data.end_time.split(" ")
                var html = '<div class="right-list-box addhomework-box mt-20 pb-150 magin-auto">\n' +
                    '<div class="items">\n' +
                    '<label>标题：</label>\n' +
                    '<input type="text" class="t t1 rad-3 ml-10 f-14" maxlength="20" id="homeworkTitle" value="'+data.data.homework_title+'"/>\n' +
                    '</div>\n' +
                    '<div class="items">\n' +
                    '<label>可见性：</label>\n' +
                    '<select class="rad-3 t t3 ml-10 f-14" id="homeworkVisibility">\n' +
                    '<option value="null">--请选择--</option>\n' +
                    '<option value="1">public</option>\n' +
                    '<option value="0">private</option>\n' +
                    '</select>\n' +
                    '</div>\n' +
                    '<div class="items">\n' +
                    '<label>班级ID：</label>\n' +
                    // '<select class="rad-3 t t3 ml-10 f-14" id="courseId"><option>--请选择--</option></select>\n' +
                    '<input type="number" class="t t7 rad-3 ml-10 f-14" maxlength="20" id="courseId" value="'+data.data.course_id+'" />\n' +
                    '</div>\n' +
                    '<div class="items">\n' +
                    '<label>开始时间：</label>\n' +
                    '<input type="date" class="t t7 rad-3 ml-10 f-14" id="startDate" placeholder="格式：2019-10-10" value="'+startTimes[0]+'"/>\n' +
                    '<input type="time" class="t t7 rad-3 ml-10 f-14" id="startTime" placeholder="格式：18:00 或 18:00:00" value="'+startTimes[1]+'"/>\n' +
                    '</div>\n' +
                    '<div class="items">\n' +
                    '<label>结束时间：</label>\n' +
                    '<input type="date" class="t t7 rad-3 ml-10 f-14" id="endDate" placeholder="格式：2019-10-10" value="'+endTimes[0]+'"/>\n' +
                    '<input type="time" class="t t7 rad-3 ml-10 f-14" id="endTime" placeholder="格式：18:00 或 18:00:00" value="'+endTimes[1]+'"/>\n' +
                    '</div>\n' +
                    '<div id="questions" class="questions">' +
                    '<div class="items">\n' +
                    '<label>作业题目：</label>' +
                    '<label></label>' +
                    '<label></label>' +
                    '<label></label>' +
                    '<label></label>' +
                    '<label></label>' +
                    '<a href="javascript:;" class="add_question btn-t c-fff f-14 bg-169 rad-3 ml-10 text-center" onclick="add_question()">增加</a>\n' +
                    '</div>\n' +
                    '</div>' +
                    '<div class="items mt-35">\n' +
                    '<label></label>\n' +
                    '<label></label>\n' +
                    '<a href="javascript:;" class="btn-t c-fff f-14 bg-169 rad-3 ml-10 text-center" onclick="getHomework()">重置</a>\n' +
                    '<label></label>\n' +
                    '<a href="javascript:;" class="btn-t c-fff f-14 bg-169 rad-3 ml-10 text-center" onclick="updateHomework()">完成编辑</a>\n' +
                    '</div>\n' +
                    '</div> ';
                getHref_path();
                $(".menu ul").hide();
                $(".menu ul").eq(3).show();
                document.getElementById("ui-right-content").innerHTML = html;
                console.log(data)
                $("#homeworkVisibility").val("1")
                for(var i=0;i<data.data.questions.length;i++){
                    add_question(data.data.questions[i].question,data.data.questions[i].score,i)
                }
            }
            else {
                $.message(data.msg)
            }
        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }

    })
}

function updateHomework(){
    var homeworkQuestion = [];
    var homeworkTitle = $("#homeworkTitle").val();
    var Visibility = parseInt($("#homeworkVisibility").val());
    var courseId = parseInt($("#courseId").val());
    var startTime = $("#startDate").val()+' '+$("#startTime").val();
    var endTime = $("#endDate").val()+' '+$("#endTime").val();
    if(homeworkTitle==""){
        $.message("请填写作业标题");
        return;
    }
    if(Visibility=="null"){
        $.message("请选择作业可见性");
        return;
    }
    if(courseId==""){
        $.message("请填写班级ID");
        return;
    }
    if($("#startDate").val()=="" || $("#startTime").val()==""){
        $.message("请填写开始时间");
        return;
    }
    if($("#endDate").val()=="" || $("#endTime").val()==""){
        $.message("请填写结束时间");
        return;
    }
    for(var i=0;i<$(".st").length;i++){
        question=$(".st").eq(i).find(".question").val();
        score=$(".st").eq(i).find(".score").val();
        if(question==""||score==""){
            $.message("请将题号和分值填写完整");
            return;
        }
        homeworkQuestion.push({
            "question":parseInt(question),
            "score":parseInt(score)
        })
    }
    if(homeworkQuestion.length==0){
        $.message("未添加题目");
        return;
    }
    $.ajax({
        url: update_homeworkLlist_interface,
        type:'post',
        dataType:'json',
        contentType:"application/json",
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:JSON.stringify({
            homeworkId:$.getQueryString("homeworkId"),
            homeworkTitle:homeworkTitle,
            homeworkVisibility:Visibility,
            courseId:courseId,
            startTime:startTime,
            endTime:endTime,
            homeworkQuestion:homeworkQuestion
        }),
        success: function(data) {
            if(data.code==200){
                $.message("完成编辑");
                setTimeout("location.href='#/homeworkList'",500);
            }else{
                $.message(data.msg);
            }
        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }
    });
}

function deleteQuestionCont(obj) {
    $(obj).closest('.questionCont').remove()
    $(".add_question").eq(-1).show()
}

function add_question(ques="",score="",flag=1) {
    $(".add_question").hide()
    var questionhtml='<div class="items questionCont">\n' +
        '<div class="st">\n' +
        '<label>题号：</label>\n' +
        '<input type="number" class="question t t3 rad-3 ml-10 f-14" value="'+ques+'"/>\n' +
        '<label>分值：</label>\n' +
        '<input type="number" class="score t t3 rad-3 ml-10 f-14" value="'+score+'"/>\n' +
        '</div>\n' +
        '<label>'
    if(flag>0){
        questionhtml+='<a class="pull-left pl-15" onclick="deleteQuestionCont(this)">删除</a>'
    }
    questionhtml+='</label>'+
        '<a href="javascript:;" class="add_question btn-t c-fff f-14 bg-169 rad-3 ml-10 text-center" onclick="add_question()">增加</a>\n' +
        '</div>\n'
    $("#questions").append(questionhtml)
}
